package com.isoft.sjhy.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.github.pagehelper.PageInfo;
import com.isoft.sjhy.model.Building;
import com.isoft.sjhy.service.BuildingService;

import com.isoft.sjhy.util.JsonObject;
import com.isoft.sjhy.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 2 * @Author: 郭邈
 * 3 * @Date: 2021/6/24 10:03
 * 4 * @ClassName:sjhy
 * 4 * @PACKAGE_NAME:com.isoft.sjhy.controller
 */
@Api(tags = {""})
@RestController
@RequestMapping("/building")
public class BuildingController {

    private Logger log = LoggerFactory.getLogger(getClass());
        @Resource
        private BuildingService buildingService;


        @RequestMapping("/queryBuildAll")
        public JsonObject queryBuildAll(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "15") Integer limit,
                                        String numbers){
            JsonObject object=new JsonObject();
            PageInfo<Building> pageInfo= buildingService.findBuildAll(page,limit,numbers);
            object.setCode(0);
            object.setMsg("ok");
            object.setCount(pageInfo.getTotal());
            object.setData(pageInfo.getList());
            return object;
        }

        @RequestMapping("/queryBuild")
        public List<Building> queryBuild(){
            PageInfo<Building> pageInfo= buildingService.findBuildAll(1,100,null);
            return pageInfo.getList();
        }


        @ApiOperation(value = "新增")
        @RequestMapping("/add")
        public R add(@RequestBody Building building){
            int num= buildingService.add(building);
            if(num>0){
                return R.ok();
            }else{
                return R.fail("添加失败");
            }

        }

        @ApiOperation(value = "删除")
        @RequestMapping("/deleteByIds")
        public R delete(String  ids){
            List<String> list= Arrays.asList(ids.split(","));
            //遍历遍历进行删除
            for(String id:list){
                buildingService.delete(Long.parseLong(id));
            }
            return R.ok();
        }

        @ApiOperation(value = "更新")
        @RequestMapping("/update")
        public R update(@RequestBody Building building){
            int num= buildingService.updateData(building);
            if(num>0){
                return R.ok();
            }else{
                return R.fail("修改失败");
            }
        }

        @ApiOperation(value = "查询分页数据")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "page", value = "页码"),
                @ApiImplicitParam(name = "pageCount", value = "每页条数")
        })
        @GetMapping()
        public IPage<Building> findListByPage(@RequestParam Integer page,
                                              @RequestParam Integer pageCount){
            return buildingService.findListByPage(page, pageCount);
        }

        @ApiOperation(value = "id查询")
        @GetMapping("{id}")
        public Building findById(@PathVariable Long id){
            return buildingService.findById(id);
        }

    }

